package com.settlement.system.service.impl;

import com.settlement.system.model.entity.OdsErpRetailSalesOrderMain;
import com.settlement.system.mapper.OdsErpRetailSalesOrderMainMapper;
import com.settlement.system.service.OdsErpRetailSalesOrderMainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.OdsErpRetailSalesOrderMainQuery;
import com.settlement.system.model.form.OdsErpRetailSalesOrderMainForm;
import com.settlement.system.model.vo.OdsErpRetailSalesOrderMainImportVo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import com.settlement.system.common.util.StringUtils;

/**
 * <p>
 * 零售销货单主表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-08-02
 */
@Service
public class OdsErpRetailSalesOrderMainServiceImpl extends ServiceImpl<OdsErpRetailSalesOrderMainMapper, OdsErpRetailSalesOrderMain> implements OdsErpRetailSalesOrderMainService {

    @Override
    public Page<OdsErpRetailSalesOrderMain> getPage(OdsErpRetailSalesOrderMainQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<OdsErpRetailSalesOrderMain> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

    @Override
    public List<OdsErpRetailSalesOrderMain> getList(OdsErpRetailSalesOrderMain odsErpRetailSalesOrderMain) {
        return this.baseMapper.getList(odsErpRetailSalesOrderMain);
    }

    @Override
     public boolean saveOdsErpRetailSalesOrderMain(OdsErpRetailSalesOrderMainForm odsErpRetailSalesOrderMainForm) {
         OdsErpRetailSalesOrderMain odsErpRetailSalesOrderMain = new OdsErpRetailSalesOrderMain();
         BeanUtils.copyProperties(odsErpRetailSalesOrderMainForm, odsErpRetailSalesOrderMain);
         return this.saveOrUpdate(odsErpRetailSalesOrderMain);
     }

     @Override
     public OdsErpRetailSalesOrderMainForm getOdsErpRetailSalesOrderMainFormData(Long id) {
         OdsErpRetailSalesOrderMain odsErpRetailSalesOrderMain = this.getById(id);
         OdsErpRetailSalesOrderMainForm odsErpRetailSalesOrderMainForm = new OdsErpRetailSalesOrderMainForm();
         BeanUtils.copyProperties(odsErpRetailSalesOrderMain, odsErpRetailSalesOrderMainForm);
         return odsErpRetailSalesOrderMainForm;
     }

     @Override
     public boolean deleteOdsErpRetailSalesOrderMain(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<OdsErpRetailSalesOrderMainImportVo> voList)  {
        List<OdsErpRetailSalesOrderMain> odsErpRetailSalesOrderMainList = new ArrayList<>();
        for (OdsErpRetailSalesOrderMainImportVo importVo : voList) {
            OdsErpRetailSalesOrderMain odsErpRetailSalesOrderMain = new OdsErpRetailSalesOrderMain();
            BeanUtils.copyProperties(importVo, odsErpRetailSalesOrderMain);
            odsErpRetailSalesOrderMainList.add(odsErpRetailSalesOrderMain);
        }
        this.saveOrUpdateBatch(odsErpRetailSalesOrderMainList);
    }

    @Override
    public List<OdsErpRetailSalesOrderMainImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<OdsErpRetailSalesOrderMain> odsErpRetailSalesOrderMainList = this.getBaseMapper().selectBatchIds(ids);
        List<OdsErpRetailSalesOrderMainImportVo> voList = new ArrayList<>();
        for (OdsErpRetailSalesOrderMain odsErpRetailSalesOrderMain : odsErpRetailSalesOrderMainList) {
            OdsErpRetailSalesOrderMainImportVo vo = new OdsErpRetailSalesOrderMainImportVo();
            BeanUtils.copyProperties(odsErpRetailSalesOrderMain, vo);
            voList.add(vo);
        }
        return voList;
    }
}
